package com.example.jucdemo.synchdemo;

/**
 * @Author: Dijah
 * @Date: 2023/3/17 14:34
 * @Desc: synchronized 的场景
 *
 * 场景：两个线程同时访问同一个对象的同步方法
 * 这个是场景安全的
 *
 */
public class demo1 implements Runnable{

    static demo1 intstance1 = new demo1();

    @Override
    public void run() {
        method();
    }

    private synchronized void method(){
        System.out.println("线程名：" + Thread.currentThread().getName() + " ,start");
        try {
            Thread.sleep(2000);
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("thread name : " + Thread.currentThread().getName() + ",end");
    }

    public static void main(String[] args) {
        Thread thread = new Thread(intstance1);

        Thread thread1 = new Thread(intstance1);
        thread.start();
        thread1.start();

    }
}
